<?php

namespace App\validate;

use app\model\UserModel;
use App\user\service\UserService;

class UserValidate extends \think\Validate
{
    protected $rule = [
        'nickname' => ['require'],
        'email' => ['require', 'email'],
        'mobile' => ['require', 'mobile'],
        'role' => ['require', 'in' => '1,2'],
        'vip' => ['require'],
        'password' => ['require', 'length' => '6,16'],

        'invite' => ['require', 'length' => '6'],
        'avatar' => ['require'],

        'code' => ['require', 'checkCode'],  // 验证码
        'againPassword' => ['require', 'length' => '6,16', 'checkAgain'],  // 确认密码
    ];

    protected $message = [
        'mobile.require' => '请输入手机号！',
        'mobile.mobile' => '手机号格式有误，请检查后重试！',
        'code.require' => '请输入验证码！',
        'email.require' => '请输入邮箱！',
        'email.email' => '邮箱格式有误，请检查后重试',
        'password.require' => '请输入登陆密码！',
        'password.length' => '密码为长度6-16位的有效字符！',
        'againPassword.require' => '请输入确认密码！',
        'againPassword.length' => '确认密码为长度6-16位的有效字符！',
    ];

    protected $scene = [
        'mobile_register' => ['mobile', 'code'],
        'email_register' => ['email', 'password', 'againPassword'],
        'mobile_login' => ['mobile', 'code'],
        'email_login' => ['email', 'password'],
        'send_code' => ['mobile'],
    ];

    protected function sceneemail_register() {
        return $this->only(['email', 'password', 'againPassword'])
            ->append('email', ['checkRepeat:email']);
    }

    protected function scenemobile_register() {
        return $this->only(['mobile', 'code'])
            ->append('mobile', ['checkRepeat:mobile']);
    }

    protected function scenemobile_login() {
        return $this->only(['mobile', 'code'])
            ->append('mobile', ['checkMobile']);
    }

    protected function sceneemail_login() {
        return $this->only(['email', 'password'])
            ->append('email', ['checkEmail']);
    }

    protected function checkRepeat($value, $rule, $data) {
        switch ($rule) {
            case 'email' :
                $str = '邮箱';
                $info = UserModel::getInfoByWhere(['email' => $value], 'id');
                break;
            case 'mobile' :
                $str = '手机号';
                $info = UserModel::getInfoByWhere(['mobile' => $value], 'id');
                break;
            case 'nickname' :
                $str = '昵称';
                $info = UserModel::getInfoByWhere(['nickname' => $value], 'id');
                break;
            default :
                return '校验类型失败, 请联系管理员:110!';
                break;
        }
       if(!empty($info) ) return '该'.$str.'已存在，请更换后重试！';
       return  true;
    }

    // 校验验证码
    protected function checkCode($value, $rule, $data) {
        $codeInfo = UserService::getCode($data['mobile']);
        $code = $codeInfo['data'];
        var_dump($value, $code);
        if ($code != $value) return '验证码有误，请检查后重新输入！';
        return true;
    }

    // 校验密码
    protected function checkAgain($value, $rule, $data) {
        if ($value !== $data['password']) return '两次输入密码不一致,请检查后重试!';
        return true;
    }

    // 登陆校验手机号
    protected function checkMobile($value, $rule, $data) {

        $info = UserModel::getInfoByWhere(['mobile' => $value],'status');
        if (empty($info)) return '用户名不存在,请检查后重试';

        $result = $this->checkStatus($info->status, '', $info);
        if ($result !== true) return $result;

        return true;
    }

    // 登陆校验密码
    protected function checkEmail($value, $rule, $data) {
        $info = UserModel::getInfoByWhere(['email' => $value],['password','status']);
        if (empty($info)) return '该邮箱尚未注册，请确认后重试！';

        if (!password_verify($data['password'], $info->password)) {
            return '账号或密码有误，请检查后重试！';
        }

        $result = $this->checkStatus($info->status, '', $info);
        if ($result !== true) return $result;

        return true;
    }

    protected function checkStatus($value, $rule, $data) {
        switch ($value) {
            case UserModel::FORBID :
                return '该账号已经被封号,请联系管理员进行解封!';
                break;
            case UserModel::LOCK :
                return '该账号已经被锁定,将于2022年2月2日 11:11:11解除锁定,若想提前解锁,请联系管理员!';
                break;
            case UserModel::WRITE_OFF :
                return '该账号已经被注销,请重新注册或联系管理员进行恢复';
                break;
        }
        return true;
    }




}